package com.longcai.cm.moudules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.cm.base.service.BaseServiceImpl;
import com.longcai.cm.moudules.system.dao.*;
import com.longcai.cm.moudules.system.domain.SysRoleMenus;
import com.longcai.cm.moudules.system.domain.SysRoles;
import com.longcai.cm.moudules.system.domain.SysUsers;
import com.longcai.cm.moudules.system.domain.SysUsersRole;
import com.longcai.cm.moudules.system.service.SysUsersService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author chenyue
 * @Title: SysUsersServiceImpl
 * @ProjectName ROOT
 * @Description: TODO
 * @date 2018/11/1416:01
 */
@Service("sysUsersService")
public class SysUsersServiceImpl extends BaseServiceImpl<SysUsers> implements SysUsersService {

    @Resource
    private SysUsersMapper sysUsersMapper;

    @Resource
    private SysRolesMapper sysRolesMapper;

    @Resource
    private SysUsersRoleMapper sysUsersRoleMapper;

    @Resource
    private SysRoleMenusMapper sysRoleMenusMapper;

    @Resource
    private SysMenusMapper sysMenusMapper;


    @Override
    public SysUsers selectByUsername(String username) {

        QueryWrapper<SysUsers> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        List<SysUsers> sysUsersList = list(wrapper);

        if (sysUsersList.size() > 0) {
            return sysUsersList.get(0);
        }
        return null;
    }


    @Override
    public SysRoles getRoleInfoByUserId(String userId) {

        QueryWrapper<SysUsersRole> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<SysUsersRole> sysUsersRoleList = sysUsersRoleMapper.selectList(wrapper);

        if (sysUsersRoleList.isEmpty()) {
            return null;
        }

        String roleIdString = sysUsersRoleList.get(0).getRoleId();
        return sysRolesMapper.selectById(roleIdString);

    }

    @Override
    public List<SysRoleMenus> getRoleMenuListByUserId(String userId) {

        String roleId = getRoleIdByUserId(userId);

        QueryWrapper<SysRoleMenus> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id", roleId);
        List<SysRoleMenus> roleMenuList = sysRoleMenusMapper.selectList(wrapper);

        return roleMenuList;
    }

    @Override
    public String getRoleIdByUserId(String userId) {

        QueryWrapper<SysUsersRole> eWrapper = new QueryWrapper<SysUsersRole>();
        eWrapper.eq("user_id", userId);
        List<SysUsersRole> sysUsersRoleList = sysUsersRoleMapper.selectList(eWrapper);
        if (sysUsersRoleList.isEmpty()) {
            return null;
        }
        return sysUsersRoleList.get(0).getRoleId();
    }


}
